package threshold;

/**
 * @Author wujiaming
 * @Description TODO
 * @Date 2023-03-01 18:42
 * @Version 1.0
 */
public class LongestCommonPre {
    public String longestCommonPrefix(String[] strs) {

        if(strs.length==1){
            return strs[0];
        }
        String temp = prefix(strs[0],strs[1]);
        for (String str:strs
             ) {
            temp = prefix(temp,str);
        }

        return temp;
    }

    public String prefix(String str1,String str2){
        String str = "";
        int len = str1.length()<str2.length()?str1.length():str2.length();
        for(int i=0;i<len;i++){
            if(str1.charAt(i)==str2.charAt(i)){
                str +=str1.charAt(i);
            }
        }
        return str;

    }

    public static void main(String[] args) {
        String[] strs = {"dog","racecar","car"};
        LongestCommonPre longestCommonPre = new LongestCommonPre();
        System.out.println(longestCommonPre.longestCommonPrefix(strs));
    }
}
